package com.zysc.processflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zysc.processflow.entity.SpecGroup;
import com.zysc.processflow.entity.SpecParam;
import com.zysc.processflow.mapper.SpecGroupMapper;
import com.zysc.processflow.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Mr.Liuyijiang
 * @Description:
 * @Date Created in 2022-06-08 22:15
 * @Modified By:
 */
@Service
public class SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 根据分类id查询参数组
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        LambdaQueryWrapper<SpecGroup> qw = new LambdaQueryWrapper<>();
        qw.eq(SpecGroup::getCid,cid);
        return this.specGroupMapper.selectList(qw);
    }
    /**
     * 根据gid查询参数组
     * @param gid
     * @return
     */
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setGeneric(generic);
        record.setSearching(searching);
        LambdaQueryWrapper<SpecParam> qw = new LambdaQueryWrapper<>();
        qw.eq(gid!=null,SpecParam::getGroupId,gid)
                .eq(cid!=null,SpecParam::getCid,cid)
                .eq(generic!=null,SpecParam::getGeneric,generic)
                .eq(searching!=null,SpecParam::getSearching,searching);
        return this.specParamMapper.selectList(qw);
    }
    /**
     * 新增组
     * @param specGroup
     * @return
     */
    public void saveGroup(SpecGroup specGroup) {
        if(specGroup.getId()!=null){
            this.specGroupMapper.updateById(specGroup);
        }else{
            this.specGroupMapper.insert(specGroup);
        }
    }

    public void saveParam(SpecParam specParam) {
        if(specParam.getId()!=null){
            this.specParamMapper.updateById(specParam);
        }else{
            this.specParamMapper.insert(specParam);
        }
    }

    public void deletegroup(String id) {
        this.specGroupMapper.deleteById(id);
    }

    public void deleteParam(String id) {
        this.specParamMapper.deleteById(id);
    }
}
